package com.itheima.health.controller;

import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "检查项模块管理")
@RestController
public class CheckItemController {

    @Reference
    private CheckItemService service;



    @GetMapping("checkitem/findAll")
    @ApiOperation(value = "查询检查项方法", notes = "查询所有的检查项列表信息")

    public Result findAll() {
        try {
            List<CheckItem> list = service.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PostMapping("checkitem/add")
    @ApiOperation(value = "添加检查项方法", notes = "添加表单检查项列表信息")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            service.saveOrUpdate(checkItem);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @PostMapping("checkitem/findPage")
    @ApiOperation(value = "分页查询", notes = "根据分页条件查询分页数据")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = service.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @DeleteMapping("checkitem/deleteCheckItemById")
    @ApiOperation(value = "删除检查项", notes = "根据id逻辑删除检查项")
    public Result deleteCheckItemById(@RequestParam("id") Integer id){
        try {
            CheckItem checkItem = new CheckItem();
            checkItem.setId(id);
            checkItem.setIs_delete(1);
            service.updateById(checkItem);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }
}
